/**
 * Created by sxx on 2017/5/1.
 */
import "./index.less";
import "../../../unit/common/js/bootstrap_js/jquery.validate.min.js";
import productEditTpl from './product_edit.handlebars';
import httpurl from "../../../unit/common/js/httpurl";

let token = sessionStorage.getItem('supplier_token');

let prodId = httpurl.uri.params.id;
let chrmIcon = '';
let ProductEdit = {
    init: function() {
        this.getInfo()
            .then(this.renderList)
            .then(this.bindEvent);
    },
    getInfo: function() {
        return new Promise((resolve, reject) => {
            $.ajax({
                url: HOST_ADDR + '/cpmp/splrSlfMng/chrm/' + prodId,
                type: 'get',
                dataType: 'json',
                headers: {
                    'access_token': token
                },
                contentType: "application/json",
                success: function(rsp) {
                    resolve(rsp);
                }
            })
        });
    },
    renderList: function(rsp) {
        console.log(rsp.data);
        rsp.data.imgFmt = HOST_ADDR + '/' + rsp.data.chrmIcon;
        let productHtml = productEditTpl(rsp.data);
        $('.J_edit-product-form').html(productHtml);
    },
    bindEvent: function() {
        $('.J_back-to-list').unbind().click(function() {
            location.href = 'supplier_chrm.html';
        });

        $('.J_imgUpload').on('click', function() {
            let formData = new FormData();
            let file = $('.J_chrmIcon')[0].files[0];
            console.log(file);
            if (file) {

                formData.append('file', file);

                $.ajax({
                    url: HOST_ADDR + '/cpmp/file/upload/img',
                    type: 'post',
                    data: formData,
                    processData: false,
                    contentType: false,
                    headers: {
                        'access_token': token,
                        'login_type': 'splr'
                    },
                    success: function(resp) {

                        if (resp.success) {
                            chrmIcon = resp.data;
                            alert('上传图片成功');
                            $('.J_imgView').attr('src', HOST_ADDR + '/' + chrmIcon);
                            //  location.href = '/supplier_self/product_list.html';
                        } else {
                            alert('上传图片失败');
                        }
                    },
                    error: function(err) {
                        console.log(err);
                    }
                });

            } else {
                alert('请先上传图片');
            }
        });
        $('.J_submit').unbind().click(function() {
            let $form = $('.J_edit-product-form');
            let id = $form.find('.J_id').val();
            let iconTitle = $form.find('.J_iconTitle').val();
            let itdc = $form.find('.J_itdc').val();
            let chrmIconAddr = chrmIcon;

            if (!iconTitle) {
                alert('请填写标题');
            } else if (!itdc) {
                alert('请填写介绍');
            } else if (!chrmIconAddr) {
                alert('请先上传图片');
            } else {

                let data = {
                    "id": id,
                    "iconTitle": iconTitle,
                    "itdc": itdc,
                    "chrmIcon": chrmIconAddr
                };

                $.ajax({
                    url: HOST_ADDR + '/cpmp/splrSlfMng/update/chrm',
                    type: 'post',
                    data: JSON.stringify(data),
                    dataType: 'json',
                    contentType: "application/json",
                    access_token: token,
                    headers: {
                        'access_token': token,
                        'login_type': 'splr'
                    },
                    success: function(resp) {
                        if (resp.success && resp.success && resp.msgCod ==='S0002') {
                            alert('修改供应商风采成功');
                            location.href = 'supplier_chrm.html';
                        } else {
                            alert(resp.msgCod+' '+resp.msgInf);
                        }
                    },
                    error: function(err) {
                        console.log(err);
                    }
                });

            }
        });
    }

}
ProductEdit.init();